<?php

namespace app\system\controller;

use app\common\tool\Tool;
use app\system\model\AdminModel;
use app\system\model\NodeModel;
use think\Controller;
use think\Db;
use think\db\Where;

class Menu extends Controller
{
    use Tool;
    //系统的菜单列表
    public function index(){

        $admin_id = input('admin_id');
        return $this->returnJson(200, '系统侧边栏菜单节点树',$this->getMenus($admin_id,0));
    }

    //根据根节点获取节点树
    //递归 获取需要被显示 show  = 1 的 菜单树, pid 为 0 表示根节点
    public function getMenus($uid = 1,$pid = 0){
        //获取节点pid 为 0 的根节点
        $where = new Where();
        //$where['status'] = 1;
        $where['show'] = 1;
        $where['pid'] = $pid;
        $rootNodes = NodeModel::where($where)->order('id', 'asc')->select();

        $menus = [];
        foreach ($rootNodes as $rootNode){
            //如果uid 为1 表示超级管理员
            //不是超级管理员并且用户没有该节点
            if($uid != 1 && Menu::hasUserTheNode($uid,$rootNode->id) === false){
                continue;
            }

            $child_node = $this->getMenus($uid,$rootNode->id);
            //只有子节点不为空才插入
            if(!empty($child_node)){
                $rootNode->child_node = $child_node;
            }
            $menus[] = $rootNode;
        }
        return $menus;
    }

    //判断用户是否拥有该节点
    public static function hasUserTheNode($uid = 0, $node_id = 0){
        $admin = AdminModel::get($uid);
        if(!$admin){
            return false;
        }
        foreach ($admin->roles as $role){
            //角色对应拥有的节点
            foreach ($role->nodes as $node){
                if($node->id === $node_id){
                    return true;
                }
            }
        }
        return false;
    }


    //根据前端当前路由查找菜单节点
    public function getNodeByRouter(){
        $router = input('router');
        $node = NodeModel::get(['router' => $router]);
        if(!$node){
            return $this->returnJson(500, '该节点不存在');
        }
        return $this->returnJson(200, '节点', $node);

    }

    public function add(){
        $pid = input('pid');
        $title = input('title');
        $router = input('router');
        $url = input('url');
        $ordering = input('ordering');
        $status = input('status');

        if(empty($pid)){
            $pid = 0;
        }
        if(empty($title)){
            return $this->returnJson(500, '菜单名称不能为空');
        }
        if(empty($router)){
            $router = '#';
        }
        if(empty($url)){
            return $this->returnJson(500, '后端路由不能为空');
        }
        if(empty($ordering)){
            $ordering = 0;
        }
        if(empty($status)){
            $status = 1;
        }

        $node = NodeModel::create([
            'pid' => $pid,
            'title' => $title,
            'router' => $router,
            'url' => $url,
            'ordering' => $ordering,
            'status' => $status,
        ]);
        if(!$node){
            return $this->returnJson(500, '插入数据库失败');
        }
        return $this->returnJson(200, '菜单创建成功',$node);
    }

    public function edit(){
        $id = input('id');
        $pid = input('pid');
        $title = input('title');
        $router = input('router');
        $url = input('url');
        $ordering = input('ordering');
        $status = input('status');

        if(empty($id)){
            return $this->returnJson(500, '菜单节点id不能为空');
        }
        if(empty($pid)){
            $pid = 0;
        }
        if(empty($title)){
            return $this->returnJson(500, '菜单名称不能为空');
        }
        if(empty($router)){
            $router = '#';
        }
        if(empty($url)){
            return $this->returnJson(500, '后端路由不能为空');
        }
        if(empty($ordering)){
            $ordering = 0;
        }
        if(!isset($status)){
            $status = 1;
        }

        $node = NodeModel::get($id);
        if(!$node){
            return $this->returnJson(500,'该菜单不存在');
        }
        $node->pid = $pid;
        $node->title = $title;
        $node->router = $router;
        $node->url = $url;
        $node->ordering = $ordering;
        $node->status = $status;
        $node->save();
        return $this->returnJson(200, '菜单修改成功',$node);
    }
    public function del(){
        $id = input('id');
        $node = NodeModel::get($id);
        if(!$node){
            return $this->returnJson(500,'该菜单不存在');
        }
        $node->delete();
        return $this->returnJson(200,'菜单删除成功');
    }
    public function change_status(){
        $id = input('id');
        $node = NodeModel::get($id);
        if(!$node){
            return $this->returnJson(500,'该菜单不存在');
        }
        $node->status = $node->status == 1 ? 0 : 1;
        $node->save();
        return $this->returnJson(200,'菜单状态修改成功',$node);
    }



}
